﻿@model IEnumerable<IAPRepo_WebRole.Models.IAPProduct>
           
@{
    ViewBag.Title = "IAP Item Repository";
}

@*<h2>@ViewBag.Message</h2>*@

<section>
    <table id="product-list">
        <thead>
            <tr>
                <th>App ID</th>
                <th>Product ID</th>
                <th>Title</th>
                <th>Description</th>
                <th>Downloadable</th>
                <th>Image</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
        @if (!Model.Any())
        {
            <tr><td colspan="7">There are currently no products stored.</td></tr>
        }
        else
        {            
            foreach (var p in Model)
            {
                <tr>
                    <td>@p.ApplicationId</td>
                    <td>@p.ProductId</td>
                    <td>@p.Title</td>
                    <td>@p.Description</td>
                    <td>
                        @if (p.DownloadableUri != null) {
                            <a href="@p.DownloadableUri">link</a>
                        } else {
                            using (Html.BeginForm("AddFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
                            {
                                <input type="hidden" name="rowKey" value="@p.RowKey" />
                                <input id="input-file" type="file" name="file" />
                                @*<input type="submit" value="Add" />*@
                            }
                        }
                    </td>
                    <td>
                        @if (p.ImageUri != null) {
                            <img class="product-image" src="@p.ImageUri" alt="Product image" />
                        } else {
                            using (Html.BeginForm("AddImage", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
                            {
                                <input type="hidden" name="rowKey" value="@p.RowKey" />
                                <input id="input-image" type="file" name="file" />
                                @*<input type="submit" value="Add" />*@
                            }
                        }
                    </td>
                    <td>
                        <a class="link-delete" rel="@p.ProductId" href="@Url.Action("Delete", new { rowKey = p.RowKey })">Delete</a>                        
                    </td>
                </tr>
            }
        }
        </tbody>
    </table>
</section>

<section id="add-product">
    <h2>Add Product</h2>

    @using (Html.BeginForm("Add", "Home", FormMethod.Post))
    {
        <table>
            <tr>
                <td><label for="input-appId">Application ID</label></td>
                <td>
                    <input id="input-appId" type="text" name="ApplicationId" value="" placeholder="A simple string used by the API" />
                </td>
            </tr>
            <tr>
                <td><label for="input-productId">Product ID</label></td>
                <td>
                    <input id="input-productId" type="text" name="ProductId" value="" placeholder="A numeric ID given by Nokia Store" />
                </td>
            </tr>
            <tr>
                <td><label for="input-title">Title</label></td>
                <td>
                    <input id="input-title" type="text" name="Title" value="" placeholder="A short title" />
                </td>
            </tr>
            <tr>
                <td><label for="input-description">Description</label></td>
                <td>
                    <input id="input-description" type="text" name="Description" value="" placeholder="An optional description" />
                </td>
            </tr>
        </table>
    
        <input id="button-save-product" type="submit" value="Save" />
    }
</section>

<script type="text/javascript">
    $(function () {
        $(".link-delete").click(function () {
            var response = confirm("Confirm deletion of Product '" + $(this).attr("rel") + "'?");
            return response;
        });

        $("#product-list input[type=file]").bind("change", function () {
            $(this).parents("form").submit();
        });
    });
</script>